<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
		<style type="text/css">
			#app ul{
				list-style: none;
				width: 60%;
				margin: auto;
				border: 1px solid black;
				padding: 10px;
			}
			#app ul li{
				display: flex;
				justify-content: space-around;
				margin-top: 10px;
				border-bottom: 1px dashed   #cccccc;
			}
			#app ul li:last-child{
				border-bottom: none;
			}
			#app ul li img{
				width: 100px;
			}
			.all{
				width: 60%;
				margin: auto;
			}
		</style>
	</head>
	<body>
		<div id="app">
			<div class="all" v-if="list.length ===0">购物车空空如也</div>
			<template v-else>
				<div class="all">
					<input type="checkbox" v-model="isChecked" @change="allCheck()"/>全选| 全不选
				</div>
				<ul>
					<li v-for="(item,index) in list" :key="item.id">
						<div>
							<input type="checkbox" v-model="checkGroup" :value="item" @change="isAllCheck()"/>
						</div>
						<div>
							<img :src="item.pic" >
						</div>
						<div>
							<div>名称：{{item.name}}</div>
							<div>价格：￥{{item.price}}</div>
						</div>
						<div>
							<button @click="item.number--" :disabled="item.number===1">-</button>
							<span>{{item.number}}</span>
							<button @click="item.number++" :disabled="item.number===item.limit">+</button>
						</div>
						<div>
							<button @click="deleteGood(index,item.id)">删除</button>
						</div>
					</li>
				</ul>
				<div class="all">总金额：{{totalPrice()}}</div>
			</template>
		</div>
		<script>
			Vue.config.productionTip = false;
			const vm = new Vue({
				el:"#app",
				data:{
					isChecked:false,
					checkGroup:[],
					list:[
						{
							name:"商品1",
							price:10,
							number:1,
							id:1,
							limit:5,//限购
							pic:"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1689157983,3552298815&fm=26&gp=0.jpg"
						},
						{
							name:"商品2",
							price:20,
							number:5,
							id:2,
							limit:8,//限购
							pic:"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1689157983,3552298815&fm=26&gp=0.jpg"
						},
						{
							name:"商品3",
							price:30,
							number:10,
							id:3,
							limit:15,//限购
							pic:"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1689157983,3552298815&fm=26&gp=0.jpg"
						},
						{
							name:"商品4",
							price:8,
							number:6,
							id:4,
							limit:18,//限购
							pic:"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1689157983,3552298815&fm=26&gp=0.jpg"
						},
						{
							name:"商品5",
							price:14,
							number:2,
							id:5,
							limit:7,//限购
							pic:"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1689157983,3552298815&fm=26&gp=0.jpg"
						}
					]
				},
				methods:{
					totalPrice(){
						var total = 0;
						this.checkGroup.forEach((ele)=>{
							total += ele.price * ele.number;
						})
						return total
					},
					deleteGood(index,goodId){
						this.list.splice(index,1);
						this.checkGroup = this.checkGroup.filter(ele => ele.id !== goodId)
						this.isAllCheck();
					},
					allCheck(){
						if(this.isChecked){
							this.checkGroup = this.list
						}else{
							this.checkGroup = [];
						}
					},
					isAllCheck(){
						if(this.checkGroup.length === this.list.length){
							this.isChecked = true;
						}else{
							this.isChecked = false;
						}
					}
				}
			})
		</script>
	</body>
</html>
